package com.caihao.onenote.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 配置跨域代理
 * @author waves
 */
@Configuration
public class CorsConfig {
    // 当前跨域请求最大有效时长，默认设置1天
    private static final long MAX_AGE = 24 * 60 * 60;

    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 配置访问原源地址，你自己的服务器ip地址
        corsConfiguration.addAllowedOrigin("http://47.97.29.31");
        corsConfiguration.addAllowedOrigin("http://localhost");
        // 设置访问源请求头
        corsConfiguration.addAllowedHeader("*");
        // 设置请求方法
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setMaxAge(MAX_AGE);
        // 对接口配置跨域设置
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);
    }
}
